﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Web.Hosting;

/// <summary>
///SilverlightUploadService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class SilverlightUploadService : System.Web.Services.WebService
{

    public SilverlightUploadService()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }


    /// <summary>
    /// 得到上传文件路径
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="firstChunk"></param>
    /// <param name="lastChunk"></param>
    /// <returns></returns>
    [WebMethod]
    public string GetServerFilePath(string fileName, bool firstChunk, bool lastChunk)
    {
        string uploadFolder = GetUploadFolder();
        //string tempFileName = fileName + _tempExtension;
        string serverFileName = uploadFolder + @"\" + fileName;
        return serverFileName;
    }

    /// <summary>
    /// 暂停上传
    /// </summary>
    /// <param name="filename"></param>
    [WebMethod]
    public void AbortUpload(string filename)
    {

    }

    private string _tempExtension = "_temp";

    [WebMethod]
    public void StoreFileAdvanced(string fileName, byte[] data, int dataLength, string parameters, bool firstChunk, bool lastChunk)
    {
        string uploadFolder = GetUploadFolder();
        string tempFileName = fileName + _tempExtension;

        if (firstChunk)
        {
            ////Delete temp file
            //if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName))
            //    File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName);

            //Delete target file
            if (File.Exists(uploadFolder + @"\" + fileName))
                File.Delete(uploadFolder + @"\" + fileName);

        }


        FileStream fs = File.Open(uploadFolder + @"\" + tempFileName, FileMode.Append);
        fs.Write(data, 0, dataLength);
        fs.Close();

        if (lastChunk)
        {
            //Rename file to original file
            File.Move(uploadFolder + @"\" + tempFileName, uploadFolder + @"\" + fileName);

            //Finish stuff....
            FinishedFileUpload(fileName, parameters);
        }

    }

    [WebMethod]
    public void CancelUpload(string fileName)
    {
        string uploadFolder = GetUploadFolder();
        string tempFileName = fileName + _tempExtension;

        if (File.Exists(uploadFolder + @"\" + tempFileName))
            File.Delete(uploadFolder + @"\" + tempFileName);

    }

    protected void DeleteUploadedFile(string fileName)
    {
        string uploadFolder = GetUploadFolder();


        if (File.Exists(uploadFolder + @"\" + fileName))
            File.Delete(uploadFolder + @"\" + fileName);
    }

    protected virtual void FinishedFileUpload(string fileName, string parameters)
    {
    }

    protected virtual string GetUploadFolder()
    {
        return @HostingEnvironment.ApplicationPhysicalPath + "/" + "Upload";
        //return @"D:\成功的例子\Upload";

    
    }

}

